﻿using System;
using System.Globalization;
using Cesave.DistributedServices.PermisosModule.WindowsHost.Resources;
using Cesave.DistributedServices.PermisosModule.WindowsHost.ViewModelBase;
using Cesave.DistributedServices.PermisosModule.WindowsHost.Views.Catalogs.Agentes;

namespace Cesave.DistributedServices.PermisosModule.WindowsHost.ViewModels.Catalogs.Agentes
{
	/// <summary>
	/// 
	/// </summary>
	public partial class AgentesViewModel
		: ObservableObject
	{
		#region Private properties

		/// <summary>
		/// 
		/// </summary>
		private AgentesView _agentesView;

        /// <summary>
        /// 
        /// </summary>
        private AgenteView _agenteView;

		#endregion

		#region Constructor

		/// <summary>
		/// 
		/// </summary>
		public AgentesViewModel(AgentesView agentesView)
		{
			if (agentesView == null)
				throw new ArgumentNullException("agentesView");
			_agentesView = agentesView;
            _agenteView = new AgenteView();
			WireUpEvents();
			InitModels();
		}

		#endregion

		#region Event methods

		/// <summary>
		/// 
		/// </summary>
		private void WireUpEvents()
		{
			PropertyChanged += AgentesViewModel_PropertyChanged;
			_agentesView.Load += LoadView;
			_agentesView.closeButton.Click += CloseView;
            _agenteView.closeButton.Click += CloseAgenteView;
            _agenteView.Load += LoadAgenteView;
			_agentesView.agentesGrid.BeforeRowsDeleted += DeleteAgente;
		}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoadAgenteView(object sender, EventArgs e)
        {
            _agenteView.Text = string.Format(
                CultureInfo.InvariantCulture,
                WindowTitles.AgentesWindowTitle
            );
        }

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void LoadView(object sender, EventArgs e)
		{
			_agentesView.Text = string.Format(
				CultureInfo.InvariantCulture,
				WindowTitles.AgentesWindowTitle
			);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
        private void CloseAgenteView(object sender, EventArgs e)
        {
			_agenteView.Hide();
        }

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void CloseView(object sender, EventArgs e)
		{
			_agentesView.Close();
		}

		#endregion

		#region Private properties

		/// <summary>
		/// 
		/// </summary>
		private void InitModels()
		{
			LoadEstados();
            LoadAcls();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		private void StartWaiting()
		{
			_agentesView.WaitingBar.StartWaiting();
		}

		/// <summary>
		/// 
		/// </summary>
		private void EndWaiting()
		{
			_agentesView.WaitingBar.EndWaiting();
		}

        /// <summary>
        /// 
        /// </summary>
        private void FocusDefaultControl()
        {
            _agenteView.ActiveControl = _agenteView.nombreUltraTextEditor;
        }

		#endregion
    }
}
